題號:7 標題;Reverse Integer 難度:Medium
我的程式碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int reverse(int x){
if(x == 0)
return 0;
if(x<0&&x<=-2147483648)
return 0;
int i,j=0,num=x;
if(x<0){
x = -(x);
j = -1;
}
int n = log10(x) + 1;
//printf("%d\n",n);
char *temp = calloc(n, sizeof(char));
char *result = calloc(n, sizeof(char));
for (i = n-1; i >= 0; --i, num /= 10)
{
temp[i] = (num % 10) + '0';
//printf("%c",temp[i]);
}
//printf("%d\n",n);
for(i=0;i<n;i++){
result[n-1-i] = temp[i];
}
x=0;
long y=0,num2=1;
for (i = 0; i < n; i++, num2*= 10)
{
y = y + (result[n-i-1]-48) *num2;
printf("xxx:%d",y);
}
if(y>0&&y>=2147483647)
return 0;
if(y<0&&y<=-2147483648)
return 0;
return y;
}
DAY26心得
今天早早寫出來,可以早早睡覺了呢~